package lee.vioson.ebook.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import lee.vioson.ebook.R
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppDestinations
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.ui.components.dialog.AlertDialog
import lee.vioson.ebook.viewmodels.MyViewModel
import lee.vioson.remotedata.manager.LoginType

/**
 *Author:viosonlee
 *Date:2023/6/3
 *DESCRIPTION:
 */
@Composable
fun MyScreen(
    navHostController: NavHostController,
    appContainer: AppContainer,
    viewModel: MyViewModel = viewModel(factory = viewModelFactory {
        initializer {
            MyViewModel(
                appContainer
            )
        }
    })
) {
    val loginResult by viewModel.loginResult.collectAsState()
    val state by viewModel.state.collectAsState()
    var showLogoutDialog by remember { mutableStateOf(false) }
    Column(
        Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
            .statusBarsPadding(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        AsyncImage(
            modifier = Modifier
                .padding(top = 40.dp, bottom = 10.dp)
                .clip(CircleShape)
                .size(60.dp),
            model = loginResult?.userInfo?.customerIcon,
            contentDescription = "user avatar",
            contentScale = ContentScale.Crop
        )
        Text(
            text = stringResource(id = R.string.Account_ID),
            fontSize = 12.sp,
            color = AppTheme.appColors.hintColor
        )
        Spacer(modifier = Modifier.height(12.dp))
        Text(
            text = loginResult?.userInfo?.email ?: "",
            fontSize = 16.sp,
            color = AppTheme.colors.onBackground
        )
        if (state.loginType == LoginType.ANONYMOUS) {
            Spacer(modifier = Modifier.height(24.dp))
            Row {
                Text(
                    text = stringResource(id = R.string.Sign_in),
                    fontSize = 14.sp,
                    color = AppTheme.appColors.createNewTextColor,
                    modifier = Modifier.clickable {
                        navHostController.navigate(AppDestinations.LOGIN_ROUTE)
                    }
                )
                Text(
                    text = stringResource(id = R.string.to_enjoy_more_benefits),
                    fontSize = 14.sp,
                    color = AppTheme.colors.onBackground
                )
            }
        }
        if (!state.isVip) {
            VipGuide(
                modifier = Modifier
                    .padding(vertical = 16.dp, horizontal = 8.dp)
                    .fillMaxWidth()
                    .height(70.dp),
                navHostController
            )
        }
        ClickItem(
            modifier = Modifier
                .fillMaxWidth()
                .height(54.dp),
            icon = Icons.Default.Settings,
            name = stringResource(id = R.string.VIP)
        ) {
            navHostController.navigate(AppDestinations.VIP_SCREEN)
        }
        ClickItem(
            modifier = Modifier
                .fillMaxWidth()
                .height(54.dp),
            icon = Icons.Default.Settings,
            name = stringResource(id = R.string.Purchase_History)
        ) {
            navHostController.navigate(AppDestinations.PURCHASE_HISTORY)
        }
        ClickItem(
            modifier = Modifier
                .fillMaxWidth()
                .height(54.dp),
            icon = Icons.Default.Settings,
            name = stringResource(id = R.string.Feedback)
        ) {
            navHostController.navigate(AppDestinations.FEEDBACK)
        }
        if (state.loginType != LoginType.ANONYMOUS) {
            ClickItem(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(54.dp),
                icon = Icons.Default.Settings,
                name = stringResource(id = R.string.Change_Password)
            ) {

            }
            ClickItem(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(54.dp),
                icon = Icons.Default.Settings,
                name = stringResource(id = R.string.Log_out)
            ) {
                showLogoutDialog = true
            }
        }
    }

    AlertDialog(
        show = showLogoutDialog,
        title = stringResource(id = R.string.logout_dialog_title),
        content = stringResource(id = R.string.logout_dialog_content),
        onDismissRequest = { showLogoutDialog = false })
    {
        showLogoutDialog = false
    }
}

@Composable
fun ClickItem(modifier: Modifier, icon: ImageVector, name: String, onClick: () -> Unit) {
    Box(modifier = modifier) {
        Row(
            modifier = Modifier
                .fillMaxSize()
                .clickable(onClick = onClick),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                modifier = Modifier.padding(horizontal = 16.dp),
                imageVector = icon, contentDescription = name,
                tint = Color(0xFF545454)
            )
            Text(
                text = name, color = AppTheme.colors.onBackground,
                fontSize = 14.sp
            )
        }
    }
}